package com.lmt.ymcc.service.impl;

import com.lmt.ymcc.service.ICreateTokenService;
import com.lmt.ymcc.utils.LoginYmccContext;
import com.lmt.ymcc.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.security.auth.login.LoginContext;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO
 * @Author: Amazing
 * @Date: 2022/8/17 16:02
 * @Version: 1.0
 */
@Service
public class CreateTokenServiceImpl implements ICreateTokenService {

    public static final String TOKEN_KEY = "token:%s+%s";
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public String createToken(String courseId) {
        Long loginId = Long loginId = LoginYmccContext.getLoginInfoDto().getId();
        String key = String.format(TOKEN_KEY, loginId, courseId);
        String token = StrUtils.getComplexRandomString(6);
        redisTemplate.opsForValue().set(key, token, 15, TimeUnit.MINUTES);
        return token;
    }
}
